package com.stephen.jsbridge.utils;

import android.text.TextUtils;

import com.stephen.jsbridge.jsbridge.CallBackFunction;

/**
 * Created by Administrator on 2016/8/5.
 */
public class JsCommandResultCallBack {
    private CallBackFunction callBackFunction;

    public JsCommandResultCallBack(CallBackFunction callBackFunction) {
        this.callBackFunction = callBackFunction;
    }

    public void success(String data){
        this.success(false,data);
    }

    public void success(boolean ignoreJson,String data){
        if(!TextUtils.isEmpty(data)){
            if(!ignoreJson && data.contains("{") && data.contains("}")){//json
                if(null != callBackFunction)callBackFunction.onCallBack("{\"isSuccess\":true,\"data\":"+data+"}");
            }else{//string
                if(null != callBackFunction)callBackFunction.onCallBack("{\"isSuccess\":true,\"data\":\""+data+"\"}");
            }
        }else{
            if(null != callBackFunction)callBackFunction.onCallBack("{\"isSuccess\":true}");
        }
    }

    public void error(String msg){
        if(!TextUtils.isEmpty(msg)){
            if(null != callBackFunction)callBackFunction.onCallBack("{\"isSuccess\":false,\"msg\":\""+msg+"\"}");
        }else{
            if(null != callBackFunction)callBackFunction.onCallBack("{\"isSuccess\":false}");
        }
    }
}